package com.chinar.funds;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.chinar.funds.until.FundDatabaseHelper;

import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private EditText et_login_username;
    private EditText et_login_password;
    private CheckBox cbRememberPassword;

    private Button btn_login_login;
    private Button btn_login_register;
    private FundDatabaseHelper dbHelper;
    
    // SharedPreferences用于存储记住的用户名和密码
    private SharedPreferences sharedPreferences;
    private static final String PREFS_NAME = "LoginPrefs";
    private static final String KEY_USERNAME = "username";
    private static final String KEY_PASSWORD = "password";
    private static final String KEY_REMEMBER = "remember";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        dbHelper = new FundDatabaseHelper(this);
        setContentView(R.layout.activity_login);
        initViews();
        
        // 初始化SharedPreferences
        sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        
        // 检查是否需要自动填充记住的用户名和密码
        checkRememberedCredentials();
        
        btn_login_register.setOnClickListener(v -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        });

        btn_login_login.setOnClickListener(v -> loginfunction());
    }

    private void checkRememberedCredentials() {
        boolean isRemembered = sharedPreferences.getBoolean(KEY_REMEMBER, false);
        if (isRemembered) {
            String rememberedUsername = sharedPreferences.getString(KEY_USERNAME, "");
            String rememberedPassword = sharedPreferences.getString(KEY_PASSWORD, "");
            
            et_login_username.setText(rememberedUsername);
            et_login_password.setText(rememberedPassword);
            cbRememberPassword.setChecked(true);
        }
    }

    private void loginfunction() {
        String username = et_login_username.getText().toString();
        String password = et_login_password.getText().toString();
        List<User> users = dbHelper.userLogin(username, password);
        Log.d("users", users.toString());
        if (users.size() == 1) {
            // 如果用户选择了记住密码，则保存用户名和密码
            if (cbRememberPassword.isChecked()) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(KEY_USERNAME, username);
                editor.putString(KEY_PASSWORD, password);
                editor.putBoolean(KEY_REMEMBER, true);
                editor.apply();
            } else {
                // 如果用户没有选择记住密码，则清除之前保存的信息
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.remove(KEY_USERNAME);
                editor.remove(KEY_PASSWORD);
                editor.putBoolean(KEY_REMEMBER, false);
                editor.apply();
            }
            
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("username", username);
            intent.putExtra("nickname", users.get(0).getNickname());
            // 注意：这里传递的密码是用户输入的明文密码，仅用于界面显示
            intent.putExtra("pwd", password);
            startActivity(intent);
        } else {
            Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
            et_login_username.setText("");
            et_login_password.setText("");
        }
    }

    private void initViews() {
        et_login_username = findViewById(R.id.login_username);
        et_login_password = findViewById(R.id.login_pwd);
        cbRememberPassword = findViewById(R.id.cb_remember_password);
        btn_login_login = findViewById(R.id.login_btn);
        btn_login_register = findViewById(R.id.register_btn);
    }
}